package com.bgy.geek.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bgy.geek.dao.*;
import com.bgy.geek.dto.LineDTO;
import com.bgy.geek.dto.ProjectDTO;
import com.bgy.geek.entity.*;
import com.bgy.geek.service.HomeService;
import com.bgy.geek.vo.PostWorkerVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页数据
 *
 * @author: dengjianwu
 * @version: 1.0
 * @date: 2021/10/22 10:19
 */
@Service
public class HomeServiceImpl implements HomeService {

	@Resource
	TbTeamDao teamDao;
	@Resource
	TbProjectDao projectDao;
	@Resource
	TbUserDao userDao;
	@Resource
	TbProductLineDao productLineDao;
	@Resource
	TbPostTypeDao  postTypeDao;
	@Resource
	HomeDao homeDao;
	/**
	 * 查询首页数量
	 * @return
	 */
	@Override
	public Map<String, Integer> homeAmount(String currentBip) {
		String rentId = homeDao.findRentId(currentBip);
		//产品线数量
		Integer productLineCount = productLineDao.selectCount(new QueryWrapper<TbProductLine>().lambda().eq(TbProductLine::getRentId,rentId));
		//研发项目数量
		Integer projectCount = projectDao.selectCount(new QueryWrapper<TbProject>().lambda().eq(TbProject::getRentId,rentId));
		//研发人员数量
		Integer userCount = userDao.selectCount(new QueryWrapper<TbUser>().lambda().eq(TbUser::getRentId,rentId));
		//研发团队数量
		Integer teamCount = teamDao.selectCount(new QueryWrapper<TbTeam>().lambda().eq(TbTeam::getRentId,rentId));
		//岗位数量
		Integer postCount = postTypeDao.selectCount(new QueryWrapper<TbPostType>().lambda().eq(TbPostType::getRentId,rentId));
		Map<String, Integer> map = new HashMap<>();
		map.put("productLineCount",productLineCount);
		map.put("projectCount",projectCount);
		map.put("userCount",userCount);
		map.put("teamCount",teamCount);
		map.put("postCount",postCount);

		return map;
	}

	/**
	 * 条线详情
	 * @return
	 */
	@Override
	public List<LineDTO> findLine(LineDTO lineDTO) {
		List<TbUser> userList = userDao.selectList(new QueryWrapper<TbUser>().lambda().eq(TbUser::getBip, lineDTO.getCurrentBip()));
		if (CollectionUtil.isEmpty(userList)) {
			throw new RuntimeException("获取用户登录信息失败");
		}
		lineDTO.setRentId(userList.get(0).getRentId());
		List<LineDTO> list = productLineDao.findLine(lineDTO);
		List<LineDTO> resultList = new ArrayList<>();
		if (CollectionUtil.isNotEmpty(list)) {
			for (LineDTO dto : list) {
				//查询条线人数
				Integer count = userDao.selectCount(new QueryWrapper<TbUser>().lambda().eq(TbUser::getLineId, dto.getId()));
				dto.setCount(count);
				resultList.add(dto);
			}
		}
		return resultList;
	}

	@Override
	public List<PostWorkerVO> findPostWorkers(String bip) {

		String rentId = homeDao.findRentId(bip);

		List<PostWorkerVO> postWorkers = homeDao.findPostWorkers(rentId);
		double total = 0;
		for (PostWorkerVO worker : postWorkers) {
			total += worker.getWorkerNum();
		}
		for (PostWorkerVO worker : postWorkers) {
			worker.setRatio(worker.getWorkerNum() / total);
		}

		return postWorkers;
	}

	/**
	 * 查询项目详情
	 * @param projectDTO
	 * @return
	 */
	@Override
	public List<ProjectDTO> findProject(ProjectDTO projectDTO) {
		String rentId = homeDao.findRentId(projectDTO.getCurrentBip());
		QueryWrapper<TbProject> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(TbProject::getRentId,Integer.valueOf(rentId));
		if (projectDTO!=null && StrUtil.isNotBlank(projectDTO.getProjectName())) {
			queryWrapper.lambda()
					.like(TbProject::getProjectCode,projectDTO.getProjectName())
					.or()
					.like(TbProject::getProjectName,projectDTO.getProjectName())
					.or()
					.like(TbProject::getActualStartTime,projectDTO.getProjectName())
					.or()
					.like(TbProject::getActualEndTime,projectDTO.getProjectName());
		}
		List<TbProject> projectList = projectDao.selectList(queryWrapper);
		//负责团队暂时写死
		List<ProjectDTO> resultList = new ArrayList<>();
		if (CollectionUtil.isNotEmpty(projectList)) {
			for (TbProject tbProject : projectList) {
				ProjectDTO dto = new ProjectDTO();

				String actualTime = tbProject.getActualStartTime() == null ? "" : tbProject.getActualStartTime().toString().substring(0,10) + " / " + (tbProject.getActualEndTime() == null ? "null" : tbProject.getActualEndTime().toString().substring(0,10));
				String planTime = tbProject.getPlanStartTime() == null ? "":tbProject.getPlanStartTime().toString().substring(0,10) + " / " + tbProject.getPlanEndTime() == null?"":tbProject.getPlanEndTime().toString().substring(0,10);
				dto
					.setProjectCode(tbProject.getProjectCode())
					.setProjectName(tbProject.getProjectName())
					.setActualTime(actualTime)
					.setPlanTime(planTime)
					.setLevel(tbProject.getLevel());
				resultList.add(dto);
			}
		}
		return resultList;
	}

}
